﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace Dmo_lc_2.Shape
{
    public class HatchShape : BaseShape
    {
        public string patternName;
        public double[] polyArray;
        public double[] boundArray;
        public string hatchType;    //填充线型

        public double[] centerPos;    //圆形/椭圆填充中心点坐标
        public double radius;   //圆形半径

        public double majorRadius;   //椭圆半径1
        public double minorRadius;   //椭圆半径2

        public HatchShape(Entity in_entity, Transaction tr) : base(in_entity, tr)
        {
        }

//        public HatchShape(Hatch in_shape, LayerTableRecord layer)
//         : base(in_shape, layer)
        public override void Init() 
        {
            Hatch in_shape = (Hatch)mEntity;

            shapeType = ShapeType.HATCH;
            shapeTypeName = shapeType.ToString();

            color = getColor();
            patternName = in_shape.PatternName;
            boundArray = new double[6];

            Extents3d boundbox = (Extents3d)in_shape.Bounds;
            boundArray[0] = boundbox.MinPoint.X;
            boundArray[1] = boundbox.MinPoint.Y;
            boundArray[2] = boundbox.MinPoint.Z;
            boundArray[3] = boundbox.MaxPoint.X;
            boundArray[4] = boundbox.MaxPoint.Y;
            boundArray[5] = boundbox.MaxPoint.Z;
            

            for (var i=0; i<in_shape.NumberOfLoops; i++)
            {
                HatchLoop hatLoop = in_shape.GetLoopAt(i);
                if (hatLoop.IsPolyline)
                {
                    polyArray = new double[hatLoop.Polyline.Count*2];
                    int j= 0;
                    foreach (BulgeVertex mypoly in hatLoop.Polyline)
                    {
                        polyArray[j*2] = mypoly.Vertex.X;
                        polyArray[j * 2+1] = mypoly.Vertex.Y;
                        j++;
                    }
                    break;
                }
                else
                {
                    if(hatLoop.Curves[0] is CircularArc2d)  //圆形
                    {
                        hatchType = "circular";
                        try
                        {
                            centerPos = new double[2];
                            foreach (CircularArc2d mypoly in hatLoop.Curves)
                            {
                                centerPos[0] = mypoly.Center.X;
                                centerPos[1] = mypoly.Center.Y;
                                radius = mypoly.Radius;
                            }
                        }
                        catch (System.Exception ex)
                        {

                            return;
                        }
                    }
                    else if (hatLoop.Curves[0] is EllipticalArc2d)  //椭圆型
                    {
                        hatchType = "elliptical";
                        try
                        {
                            centerPos = new double[2];
                            foreach (EllipticalArc2d mypoly in hatLoop.Curves)
                            {
                                centerPos[0] = mypoly.Center.X;
                                centerPos[1] = mypoly.Center.Y;
                                majorRadius = mypoly.MajorRadius;
                                minorRadius = mypoly.MinorRadius;
                            }
                        }
                        catch (System.Exception ex)
                        {

                            return;
                        }
                    }
                    else if(hatLoop.Curves[0] is LineSegment2d) //线段
                    {
                        hatchType = "line";
                        try
                        {
                            polyArray = new double[hatLoop.Curves.Count * 2 + 2];
                            int j = 0;
                            foreach (LineSegment2d mypoly in hatLoop.Curves)
                            {
                                polyArray[j * 2] = mypoly.StartPoint.X;
                                polyArray[j * 2 + 1] = mypoly.StartPoint.Y;
                                j++;
                            }
                            polyArray[polyArray.Length - 2] = polyArray[0];
                            polyArray[polyArray.Length - 1] = polyArray[1];
                        }
                        catch (System.Exception ex)
                        {

                            return;
                        }
                        
                        
                    }
                    
                }
            }
            
        }
    }
}